//获取应用实例
var app = getApp();

Page({
    data: {
        goods_list: [],
        default_address: {},
        yun_price: "0.00",
        pay_price: "0.00",
        total_price: "0.00",
        params: null,
        express_address_id: 0
    },
    onShow: function () {
        var that = this;
        that.getOrderInfo();
    },
    onLoad: function (e) {
        var that = this;
        that.setData({
            params: JSON.parse(e.data)
        })
    },
    createOrder: function (e) {
        wx.showLoading();
        const that = this;
        const data = {
            type: that.data.params.type,
            goods: JSON.stringify(that.data.params.goods),
            express_address_id: that.data.default_address.id
        };
        wx.request({
            url: app.buildUrl("/order/create"),
            header: app.getRequestHeader(),
            method: 'POST',
            data: data,
            success: function (res) {
                wx.hideLoading();
                const resp = res.data;
                if (resp.code === 200) {
                    wx.navigateTo({
                        url: "/pages/my/order_list?data=" + + JSON.stringify(data)
                    });
                } else {
                    app.alert({'content': resp.msg})
                }
            }
        })
    },
    addressSet: function () {
        wx.navigateTo({
            url: "/pages/my/addressSet"
        });
    },
    selectAddress: function () {
        wx.navigateTo({
            url: "/pages/my/addressList"
        });
    },
    getOrderInfo: function () {
        const that = this;
        const data = {
            type: that.data.params.type,
            goods: JSON.stringify(that.data.params.goods)
        };
        wx.request({
            url: app.buildUrl("/order/info"),
            header: app.getRequestHeader(),
            method: 'POST',
            data: data,
            success: function (res) {
                const resp = res.data;
                if (resp.code === 200) {
                    that.setData({
                        goods_list: resp.data.food_list,
                        default_address: resp.data.default_address,
                        pay_price: resp.data.pay_price,
                        total_price: resp.data.total_price,
                        yun_price: resp.data.yun_price,
                    })
                    if (that.data.default_address) {
                        that.setData({
                            express_address_id: that.data.default_address.id
                        })
                    }
                } else {
                   app.alert({"content": resp.msg})
                }
            }
        })
    }
});
